<!-- <!DOCTYPE html> -->
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .search {
            position: absolute;
            width: 400px;
            height: 50px;
            background: red;
            top: 50px;
            left: 50%;
            transform: translate(-50%);
        }

        .content {
            width: 100px;
            height: 2000px;
            background: blue;
        }
    </style>
</head>

<body>
    <div class="search"></div>
    <div class="content"></div>
    <button>点击回到顶部</button>
    <script>

        // 我的  ： 获取到的是2个数值 必然会有一个是0 那么这个就是false 另一个是 具体的值 用|| 连接的时候会走具体的值
        // var sTop = document.documentElement.scrollTop || document.body.scrollTop;
        // var sTop = ele.scrollTop;
        // 同学的
        // console.log(document.documentElement.scrollTop,document.body.scrollTop);
        // var ele  = true || true;
        // 或者“||” 的左右始终是true  因为document.documentElement 和document.body 不管是加或者不加doctype都能获取到
        // var ele = document.documentElement || document.body;

        // 推荐 ：
        // 判断是否有doctype标签
        // console.log(document.doctype);
        var ele;
        if (document.doctype) {
            ele = document.documentElement;
        } else {
            ele = document.body;
        }
        // console.log(ele)
        var searchEle = document.getElementsByClassName("search")[0];



        window.onscroll = function () {
            // console.log(document.documentElement.scrollTop,document.body.scrollTop);
            // console.log("滚了");
            // var ele = document.documentElement || document.body;
            // var sTop = ele.scrollTop;
            // console.log(sTop)
            // var ele = document.documentElement || document.body;
            // var sTop = ele.scrollTop;
            // console.log(sTop);
            // 判断 卷进入的距离 
            // console.log(ele.scrollTop);
            if (ele.scrollTop > 50) {
                searchEle.style.position = "fixed";
                searchEle.style.top = 0;
            } else {
                searchEle.style.position = "absolute";
                searchEle.style.top = "50px";
            }

        }

        var btn = document.getElementsByTagName("button")[0];
        btn.onclick = function(){
            console.log("回到顶部");
            // ele.scrollTop = 0;
            console.log(ele.scrollTop);
          var timer =   setInterval(function(){
              console.log("定时器");
                ele.scrollTop -= 20;
                // 停止定时器 
                if(ele.scrollTop<=0){
                    ele.scrollTop = 0;
                    clearInterval(timer);
                }
               
            },10)
        }


    </script>
</body>

</html>